package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.BossJobEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.BossJobService;

@Controller
@RequestMapping("/web")
/**
 * boss 职务
 * @author X
 *
 */
public class WebBossJobController {

    @Resource(name="bossJobService")
    private BossJobService bossJobService;
    
    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping(value = "/toBossJobIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toBossJobIndex() {
        ModelAndView mav = new ModelAndView("/boss/boss_zhiwu");
        
        return mav;
    }
    
    /**
     * 数据展示
     * @param request
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "/toListAllBossJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllBossJob(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = bossJobService.getListBossJobCount(name);
        
        //根据条件查询当前页数据
        List<BossJobEntity> list = bossJobService.getAllBossJob(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<BossJobEntity> page = new Page<BossJobEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<BossJobEntity> pageModel = new PageModelList<BossJobEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 添加
     */
    @RequestMapping(value = "/toSaveBossJob")
    @ResponseBody
    public int toSaveBossJob(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        BossJobEntity bossJob = new BossJobEntity();
        
        bossJob.setName(name);
        
        int flag = bossJobService.saveBossJob(bossJob);
        
        return flag;
    }
    
    /**
     * 删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteBossJobId")
    @ResponseBody
    public int toDeleteBossJobId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = bossJobService.deleteBossJobId(id);
        return flag;
    }
    
    /**
     * 修改
     */
    
    @RequestMapping(value = "/toUpdateBossJobId")
    @ResponseBody
    public int toUpdateBossJobId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        
        BossJobEntity bossJob = new BossJobEntity();
        
        bossJob.setName(name);
        bossJob.setJobId(id);
        
        int flag = bossJobService.updateBossJobId(bossJob);
        return flag;
    }
}
